<template>
  <el-dialog :close-on-click-modal="false" :visible.sync="visible">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      size="small"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="工作单信息" name="first">

          <el-form-item label="采购仓库" prop="wareId">
            <el-select
              v-model="dataForm.wareId"
              placeholder="请选择采购仓库"
              clearable
            >
              <el-option
                :label="w.name"
                :value="w.id"
                v-for="w in wareList"
                :key="w.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="付款方式" prop="paymentWay">
            <el-select
              v-model="dataForm.paymentWay"
              placeholder="请选择付款方式"
            >
              <el-option
                :label="w.name"
                :value="w.id"
                v-for="w in paymentWayList"
                :key="w.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="工作单备注" prop="taskComment">
            <el-input
              type="textarea"
              v-model="dataForm.taskComment"
              placeholder="工作单备注"
            ></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="订单信息" name="second">
          <el-form-item label="物流单号" prop="trackingNo">
            <el-input
              v-model="dataForm.trackingNo"
              placeholder="物流单号"
            ></el-input>
          </el-form-item>
          <el-form-item label="收货人" prop="consignee">
            <el-input
              v-model="dataForm.consignee"
              placeholder="收货人"
            ></el-input>
          </el-form-item>
          <el-form-item label="收货人电话" prop="consigneeTel">
            <el-input
              v-model="dataForm.consigneeTel"
              placeholder="收货人电话"
            ></el-input>
          </el-form-item>
          <el-form-item label="配送地址" prop="deliveryAddress">
            <el-input
              v-model="dataForm.deliveryAddress"
              placeholder="配送地址"
            ></el-input>
          </el-form-item>

          <el-form-item label="订单描述" prop="orderBody">
            <el-input
              type="textarea"
              v-model="dataForm.orderBody"
              placeholder="订单描述"
            ></el-input>
          </el-form-item>

          <el-form-item label="订单备注" prop="orderComment">
            <el-input
              type="textarea"
              v-model="dataForm.orderComment"
              placeholder="订单备注"
            ></el-input>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { wareOrderTaskSaveAndUpdate,wareInfoList } from "@/api/ware";
export default {
  data() {
    return {
      visible: false,
      activeName: "first",
      wareList:[],
      paymentWayList: [
        {
          id: 1,
          name: "在线付款",
        },
        {
          id: 2,
          name: "货到付款",
        },
      ],
      dataForm: {
        id: 0,
        orderId: "",
        orderSn: "",
        consignee: "",
        consigneeTel: "",
        deliveryAddress: "",
        orderComment: "",
        paymentWay: "",
        taskStatus: "",
        orderBody: "",
        trackingNo: "",
        wareId: "",
        taskComment: "",
      },
      dataRule: {
        orderId: [
          { required: true, message: "order_id不能为空", trigger: "blur" },
        ],
        orderSn: [
          { required: true, message: "order_sn不能为空", trigger: "blur" },
        ],
        consignee: [
          { required: true, message: "收货人不能为空", trigger: "blur" },
        ],
        consigneeTel: [
          { required: true, message: "收货人电话不能为空", trigger: "blur" },
        ],
        deliveryAddress: [
          { required: true, message: "配送地址不能为空", trigger: "blur" },
        ],
        orderComment: [
          { required: true, message: "订单备注不能为空", trigger: "blur" },
        ],
        paymentWay: [
          {
            required: true,
            message: "付款方式【 1:在线付款 2:货到付款】不能为空",
            trigger: "blur",
          },
        ],
        taskStatus: [
          { required: true, message: "任务状态不能为空", trigger: "blur" },
        ],
        orderBody: [
          { required: true, message: "订单描述不能为空", trigger: "blur" },
        ],
        trackingNo: [
          { required: true, message: "物流单号不能为空", trigger: "blur" },
        ],
        wareId: [
          { required: true, message: "仓库id不能为空", trigger: "blur" },
        ],
        taskComment: [
          { required: true, message: "工作单备注不能为空", trigger: "blur" },
        ],
      },
    };
  },
 created() {
    this.getWares();
  },
  methods: {
    getWares() {
      wareInfoList().then((res) => {
        if (res && res.code === 0) {
          this.wareList = res.data;
        } else {
          this.wareList = [];
        }
      });
    },
    init(data) {
      this.dataForm.id = 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (data) {
          this.dataForm.orderId = data.orderId;
          this.dataForm.orderSn = data.orderSn;
          this.dataForm.consignee = data.consignee;
          this.dataForm.consigneeTel = data.consigneeTel;
          this.dataForm.deliveryAddress = data.deliveryAddress;
          this.dataForm.orderComment = data.orderComment;
          this.dataForm.paymentWay = data.paymentWay;
          this.dataForm.taskStatus = data.taskStatus;
          this.dataForm.orderBody = data.orderBody;
          this.dataForm.trackingNo = data.trackingNo;
          this.dataForm.wareId = data.wareId;
          this.dataForm.taskComment = data.taskComment;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          wareOrderTaskSaveAndUpdate({
            id: this.dataForm.id || "",
            orderId: this.dataForm.orderId,
            orderSn: this.dataForm.orderSn,
            consignee: this.dataForm.consignee,
            consigneeTel: this.dataForm.consigneeTel,
            deliveryAddress: this.dataForm.deliveryAddress,
            orderComment: this.dataForm.orderComment,
            paymentWay: this.dataForm.paymentWay,
            taskStatus: this.dataForm.taskStatus,
            orderBody: this.dataForm.orderBody,
            trackingNo: this.dataForm.trackingNo,
            wareId: this.dataForm.wareId,
            taskComment: this.dataForm.taskComment,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
